/*
 * ObjectGame: Enemy
 */

package demo;

import java.awt.Graphics;
import java.awt.Point;
import javaPlay.GameObject;
import javaPlay.Sprite;

/**
 * @author VisionLab/PUC-Rio
 */
public class Enemy extends GameObject {
	
	private Sprite sprite;
	
	private Point pathEndPoint1;
	
	private Point pathEndPoint2;
	
	private boolean movingToEndPoint2;
	
	private final int UP_FRAME = 0;
	
	private final int LEFT_FRAME = 2;
	
	private final int RIGHT_FRAME = 6;
	
	private final int DOWN_FRAME = 4;
	
	private int currFrame;
	
	public void setSprite(Sprite sprite) {
		this.sprite = sprite;
	}
	
	public void setPath(Point endPoint1, Point endPoint2) {
		pathEndPoint1 = endPoint1;
		pathEndPoint2 = endPoint2;
		x = pathEndPoint1.x;
		y = pathEndPoint2.y;
		movingToEndPoint2 = true;
	}
	
	public void step(long timeElapsed) {
		Point endPoint;
		
		if (movingToEndPoint2 == true) {
			endPoint = pathEndPoint2;
		} else {
			endPoint = pathEndPoint1;
		}
		
		if (x < endPoint.x) {
			x++;
			currFrame = RIGHT_FRAME;
		} else if (x > endPoint.x) {
			x--;
			currFrame = LEFT_FRAME;
		}
		
		if (y < endPoint.y) {
			y++;
			currFrame = UP_FRAME;
		} else if (y > endPoint.y) {
			y--;
			currFrame = DOWN_FRAME;
		}
		
		if ((x == endPoint.x) && (y == endPoint.y)) {
			movingToEndPoint2 = !movingToEndPoint2;
		}
	}
	
	public void draw(Graphics g) {
		sprite.setCurrentAnimationFrame(currFrame);
		sprite.draw(g, x, y);
	}
}
